package com.cssw.fyzb.busi.api.video;

import com.cssw.fyzb.core.model.vo.video.AddVideoVo;
import com.cssw.fyzb.busi.service.video.VideoService;
import com.cssw.fyzb.core.exception.BizException;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 添加视频
 *
 * @author YangYiQun
 */
@Function(description = "上传本地视频", parameters = {
        @Parameter(name = "userId", type = Type.Integer, description = "用户标识", required = true),
        @Parameter(name = "title", type = Type.String, description = "主题", required = true),
        @Parameter(name = "media", type = Type.String, description = "视频地址", required = true),
        @Parameter(name = "cover", type = Type.String, description = "视频封面", required = true),
        @Parameter(name = "lat", type = Type.String, description = "纬度", required = true),
        @Parameter(name = "lng", type = Type.String, description = "经度", required = true),
}, result = @Result(properties = {
        @Property(name = "id", type = Type.Integer, description = "视频标识"),
}))
@Component
public class AddLocalVideo implements Action {

    @Autowired
    private VideoService videoService;

    private static final Logger logger = LoggerFactory.getLogger(AddLocalVideo.class);

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        String title = request.getString("title");
        String media = request.getString("media");
        String cover = request.getString("cover");
        String lat = request.getString("lat");
        String lng = request.getString("lng");
        try {
            Long id = videoService.addVideoByLocal(userId, title, media, cover, lat, lng);
            return Response.success().put("data", new AddVideoVo(id));
        } catch (Exception e) {
            if (e instanceof BizException) {
                BizException be = (BizException) e;
                return Response.fail(be.getErrMsg(), be.getErrCode());
            }
            logger.info("", e);
            return Response.fail("操作异常");
        }
    }

}
